<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/7
 * Time: 19:22
 */

namespace app\api\controller;



use think\Db;

class Dyn extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function _initialize()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }

    
    /**
     * 举报类型
     * @return \think\response\Json
     */
    public function reportType(){
        $list = Db::table('up_report_type')->field('rt_id, rt_name')->select();
        if($list){
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 举报
     * @return \think\response\Json
     * report_hot_id      被举报的动态ID
     * report_type_id     举报类型  逗号隔开
     * report_content     举报内容
     * report_image       举报图片
     */
    public function report(){
        $report_hot_id = input('report_hot_id');
        $report_type_id = input('report_type_id');
        $report_content = input('report_content');
        $report_image = input('report_image');
        $insert = [
            'report_hot_id' => $report_hot_id,
            'report_type_id' => $report_type_id,
            'report_content' => $report_content,
            'report_image' => $report_image,
            'report_user_id' => $this->user_id,
            'report_time' => $this->create_time
        ];
        $res = Db::table('up_report')->insert($insert);
        if($res){
            return json(['status' => 200, 'msg' => '提交成功']);
        }else{
            return json(['status' => 0, 'msg' => '提交失败']);
        }
    }

    /**
     * 发布评论
     * @return \think\response\Json
     * commnet_hot_id    动态ID
     * comment_content   评论内容
     */
    public function writeReport(){
        $comment_hot_id = input('hot_id');
        $comment_content = input('comment_content');
        $comment_see = input('comment_see');
        $insert = [
            'comment_hot_id' => $comment_hot_id,
            'comment_content' => $comment_content,
            'comment_reply_user_id' => $this->user_id,
            'comment_see' => $comment_see,
            'comment_time' => $this->create_time
        ];
        $have = Db::table('up_hot')->where('h_id', $comment_hot_id)
            ->field('h_id,h_user_id,h_content')->find();
        if(empty($have)){
            return json(['status' => 0, 'msg' => '此条动态已不存在']);
        }
        Db::startTrans();
        try{
            // 评论
            Db::table('up_comment')->insert($insert);
            // 评论数量加一
            Db::table('up_hot')->where('h_id', $comment_hot_id)->setInc('h_message');
            //
            $senduid = $have['h_user_id'];
            $title = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_username');
            $title .= ' 评论了您的动态';
            if($comment_see == 2){
                $title = '您的动态有新的评论';
            }
            allInNotice($comment_content,$have['h_content'],2,$comment_hot_id,$comment_hot_id,$senduid,$comment_see,$this->user_id);
            Db::commit();
            try{
                $this->Jpush(1, $senduid, $title, ['n_id' => $comment_hot_id,'n_type' => 2]);
            }catch(\Exception $e){

            }
            return json(['status' => 200, 'msg' => '评论成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '评论失败']);
        }
    }
    
    /**
     * 回复评论
     * @return \think\response\Json
     * comtt_comment_id     评论ID
     * comtt_content        回复的内容
     */
    public function replyToComment(){
        $comtt_comment_id = input('comment_id');
        $comtt_content = input('comtt_content');
        $comtt_see = input('comtt_see');
        $insert = [
            'comtt_comment_id' => $comtt_comment_id,
            'comtt_one_user_id' => $this->user_id,
            'comtt_content' => $comtt_content,
            'comtt_see' => $comtt_see,
            'comtt_time' => $this->create_time
        ];
        $res = Db::table('up_comment_tt')->insert($insert);
        if($res){
            Db::table('up_comment')->where('comment_id', $comtt_comment_id)->setInc('comment_num');
            $send = Db::table('up_comment')->where('comment_id', $comtt_comment_id)
                ->field('comment_reply_user_id,comment_hot_id,comment_content')->find();
            $title = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_username');
            $title .= ' 回复了您的评论';
            if($comtt_see == 2){
                $title = '您有新的回复';
            }
            allInNotice($comtt_content,$send['comment_content'],3,$comtt_comment_id,$send['comment_hot_id'],$send['comment_reply_user_id'],$comtt_see,$this->user_id);
            try{
                $this->Jpush(1, $send['comment_reply_user_id'], $title, ['n_id' => $send['comment_hot_id'],'n_type' => 3]);
            }catch(\Exception $e){

            }
            return json(['status' => 200, 'msg' => '回复成功']);
        }else{
            return json(['status' => 0, 'msg' => '回复失败']);
        }
    }
    

    
    /**
     * 对回复进行回复
     * @return \think\response\Json
     * comtt_id     回复ID
     * comtt_content   回复的内容
     */
    public function replyToReply(){
        $comtt_id = input('comtt_id');
        $comtt_content = input('comtt_content');
        $one_id = input('one_id');
        $comtt_see = input('comtt_see');
        // 被回复的记录
        $reply_info = Db::table('up_comment_tt')
            ->join('up_u_user', 'user_id = comtt_one_user_id', 'left')
            ->where('comtt_id', $comtt_id)
            ->field('comtt_comment_id, comtt_content, user_username,comtt_one_user_id,comtt_see')
            ->find();
//        $content = $comtt_content . '//@' . $reply_info['user_username'] . ':' . $reply_info['comtt_content'];
        $insert = [
            'comtt_comment_id' => $reply_info['comtt_comment_id'],
            'comtt_one_user_id' => $this->user_id,
            'comtt_reply_user_id' => $reply_info['comtt_one_user_id'],
            'comtt_content' => $comtt_content,
            'comtt_reply_content' => $reply_info['comtt_content'],
            'comtt_see' => $comtt_see,
            'comtt_reply_see' => $reply_info['comtt_see'],
            'comtt_time' => $this->create_time
        ];
        $res = Db::table('up_comment_tt')->insert($insert);
        if($res){
            Db::table('up_comment')->where('comment_id', $reply_info['comtt_comment_id'])->setInc('comment_num');

            $hot_id = Db::table('up_comment')->where('comment_id', $reply_info['comtt_comment_id'])
                ->value('comment_hot_id');
            $title = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_username');
            $title .= ' 回复了您的回复';
            if($comtt_see == 2){
                $title = '您有新的回复';
            }
            allInNotice($comtt_content,$reply_info['comtt_content'],4,$reply_info['comtt_comment_id'],$hot_id,$reply_info['comtt_one_user_id'],$comtt_see,$this->user_id);
            try{
                $this->Jpush(1, $reply_info['comtt_one_user_id'], $title, ['n_id' => $hot_id,'n_type' => 4]);
            }catch(\Exception $e){

            }
            return json(['status' => 200, 'msg' => '回复成功']);
        }else{
            return json(['status' => 0, 'msg' => '回复失败']);
        }
    }
    /**
     * 打赏
     * @return \think\response\Json
     *
     */
    public function giveMoney()
    {
        $uid = $_POST['uid'];
        $money = (int)$_POST['money'];
        $user_id = $_POST['user_id'];
        if($money <= 0){
            return json(['status' => 0, 'msg' => '打赏的怼币不能小于0']);
        }
        if($uid == $user_id){
            return json(['status' => 0, 'msg' => '不能自己给自己打赏']);
        }
        $user = Db::table('up_u_user')->where('user_id', $user_id)->field('user_money')->find();
        if($user['user_money'] < $money){
            return json(['status' => 100, 'msg' => '您的怼币不足，请先去充值']);
        }
        Db::startTrans();
        try{
            Db::table('up_u_user')->where('user_id', $user_id)->setDec('user_money',$money);
            Db::table('up_u_user')->where('user_id', $uid)->setInc('user_money',$money);
            //原先有多少，加进去
            Db::table('up_u_user')->where('user_id', $uid)->setInc('user_money_oldall',$money);
            // 入记录
            allInRevenue(2,$money,$user_id,$uid);
            Db::commit();
            return json(['status' => 200, 'msg' => '打赏成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => '失败']);
        }
    }
    /**
     * 某用户的关注（包括自己）
     * @return \think\response\Json
     * page   页码
     */
    public function myLike(){
        $uid = $_POST['uid'];
        $user_id = $_POST['user_id'];
        $users = Db::table('up_like')->alias('l')
            ->join('up_u_user u', 'l.l_other_user_id = u.user_id', 'left')
            ->where('l_user_id', $uid)
            ->order('l_time desc')
            ->page($this->page, 20)
            ->field('u.user_username,u.user_desc,u.user_image,u.user_id')
            ->select();
        if($users){
            foreach ($users as $k => $v){
                $users[$k]['eachlike'] = 0; //我没有关注
                $ihave = Db::table('up_like')
                    ->where('l_user_id',$user_id)
                    ->where('l_other_user_id',$v['user_id'])
                    ->find();
                $have = Db::table('up_like')
                    ->where('l_user_id',$v['user_id'])
                    ->where('l_other_user_id',$user_id)
                    ->find();
                if($ihave){
                    $users[$k]['eachlike'] = 1; //我关注了
                }
                if($ihave && $have){
                    $users[$k]['eachlike'] = 2; //互相关注
                }
            }
        }
        return json(['status' => 200, 'msg' => $users]);
    }
    /**
     * 某用户的粉丝（包括自己）
     * @return \think\response\Json
     * page   页码
     */
    public function myFans(){
        $uid = $_POST['uid'];
        $user_id = $_POST['user_id'];
        $users = Db::table('up_like')->alias('l')
            ->join('up_u_user u', 'l.l_user_id = u.user_id', 'left')
            ->where('l_other_user_id', $uid)
            ->order('l_time desc')
            ->page($this->page, 20)
            ->field('u.user_username,u.user_desc,u.user_image,u.user_id')
            ->select();
        if($users){
            foreach ($users as $k => $v){
                $users[$k]['eachlike'] = 0; //我没有关注
                $ihave = Db::table('up_like')
                    ->where('l_user_id',$user_id)
                    ->where('l_other_user_id',$v['user_id'])
                    ->find();
                $have = Db::table('up_like')
                    ->where('l_user_id',$v['user_id'])
                    ->where('l_other_user_id',$user_id)
                    ->find();
                if($ihave){
                    $users[$k]['eachlike'] = 1; //我关注了
                }
                if($ihave && $have){
                    $users[$k]['eachlike'] = 2; //互相关注
                }
            }
        }
        return json(['status' => 200, 'msg' => $users]);
    }
    
    /**
     * 用户主页
     * @return \think\response\Json
     * other_user_id    用户ID
     * page    页码
     */
    public function userHomePage(){
        $other_user_id = input('uid');
        // 用户基本信息
        $user_info = Db::table('up_u_user')
            ->where('user_id', $other_user_id)
            ->find();
        //------------------------------------------- 等级核实 start --------------------------------------------------------------
        $level = Db::table('up_user_level')
            ->where('level_min','<=',$user_info['user_money_oldall'])
            ->where('level_max','>=',$user_info['user_money_oldall'])
            ->field('level_id,level_name')
            ->find();
        if($level['level_id'] != $user_info['user_level_id']){
            Db::table('up_u_user')->where('user_id',$other_user_id)->update([
                'user_level_id' => $level['level_id'],
                'user_level_name' => $level['level_name']
            ]);
        }
        //-------------------------------------------- 等级核实 end -------------------------------------------------------------
        // 我是否关注该用户
        $isLike = Db::table('up_like')
            ->where('l_user_id', $this->user_id)
            ->where('l_other_user_id', $other_user_id)
            ->find();
        $like = 0;
        if($isLike){
            $like = 1; // 已关注
        }
        $user_info['like'] = $like;
        
        // 该用户关注的人数
        $likeNum = Db::table('up_like')->where('l_user_id', $other_user_id)->count();
        $user_info['likeNum'] = $likeNum;
        
        // 该用户的粉丝数
        $followNum = Db::table('up_like')->where('l_other_user_id', $other_user_id)->count();
        $user_info['followNum'] = $followNum;
        //该用户打赏出去的
        $gives = Db::table('up_revenue')->where('revenue_user_id', $other_user_id)->sum('revenue_money');
        $user_info['giveMoney'] = $gives;
        //该用户得到的
        $gets = Db::table('up_revenue')->where('revenue_other_id', $other_user_id)->sum('revenue_money');
        $user_info['getMoney'] = $gets;
        return json(['status' => 200, 'msg' => $user_info]);
    }
    /**
     * 某用户的帖子列表
     * @return \think\response\Json
     * other_user_id    用户ID
     * page    页码
     */
    public function userHots(){
        $uid = input('uid');
        $user_id = input('user_id');
        $page = input('page');
        if(empty($uid) || !$uid){
            $uid = $user_id;
        }
        $where = [
            'h_status' => 1 ,
            'h_user_id' => $uid ,
        ];
        $list = Db::table('up_hot h')
            ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
            ->join('up_hot_type t', 'h.h_type = t.type_id', 'left')
            ->field('h.*, user_username, user_image, user_time, user_id,user_level_name, type_name, type_image')
            ->where($where)
            ->page($page, 10)
            ->order('h_update_time desc')
            ->select();
        if($list){
            $list = $this->getHotsByData($list,$uid);
        }
        return json(['status' => 200, 'msg' => $list]);
    }
    /**
     * 某用户收藏的帖子列表
     * @return \think\response\Json
     * other_user_id    用户ID
     * page    页码
     */
    public function userLikeHots(){
        $uid = input('uid');
        $user_id = input('user_id');
        $page = input('page');
        if(empty($uid) || !$uid){
            $uid = $user_id;
        }
        $where = [
            'h_status' => 1 ,
            'lh_user_id' => $uid ,
        ];
        $list = Db::table('up_like_hot lh')
            ->join('up_hot h', 'lh.lh_h_id = h.h_id', 'left')
            ->join('up_u_user u', 'h.h_user_id = u.user_id', 'left')
            ->join('up_hot_type t', 'h.h_type = t.type_id', 'left')
            ->field('h.*, user_username, user_image, user_time, user_id, user_level_name, type_name, type_image')
            ->where($where)
            ->page($page, 10)
            ->order('h_update_time desc')
            ->select();
        if($list){
            $list = $this->getHotsByData($list,$uid);
        }
        return json(['status' => 200, 'msg' => $list]);
    }
}